
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>什么是微服务？微服务之间是如何独立通讯的？ · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="choose-microservice-deployment-strategy.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高并发架构</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../high-concurrent/mq-interview.html">
            
                <a href="../high-concurrent/mq-interview.html">
            
                    
                    消息队列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../high-concurrent/why-mq.html">
            
                <a href="../high-concurrent/why-mq.html">
            
                    
                    为什么使用消息队列？消息队列有什么优点和缺点？Kafka、ActiveMQ、RabbitMQ、RocketMQ 都有什么优点和缺点？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../high-concurrent/how-to-ensure-high-availability-of-message-queues.html">
            
                <a href="../high-concurrent/how-to-ensure-high-availability-of-message-queues.html">
            
                    
                    如何保证消息队列的高可用？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../high-concurrent/how-to-ensure-that-messages-are-not-repeatedly-consumed.html">
            
                <a href="../high-concurrent/how-to-ensure-that-messages-are-not-repeatedly-consumed.html">
            
                    
                    如何保证消息不被重复消费？（如何保证消息消费的幂等性）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../high-concurrent/how-to-ensure-the-reliable-transmission-of-messages.html">
            
                <a href="../high-concurrent/how-to-ensure-the-reliable-transmission-of-messages.html">
            
                    
                    如何保证消息的可靠性传输？（如何处理消息丢失的问题）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="../high-concurrent/how-to-ensure-the-order-of-messages.html">
            
                <a href="../high-concurrent/how-to-ensure-the-order-of-messages.html">
            
                    
                    如何保证消息的顺序性？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="../high-concurrent/mq-time-delay-and-expired-failure.html">
            
                <a href="../high-concurrent/mq-time-delay-and-expired-failure.html">
            
                    
                    如何解决消息队列的延时以及过期失效问题？消息队列满了以后该怎么处理？有几百万消息持续积压几小时，说说怎么解决？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="../high-concurrent/mq-design.html">
            
                <a href="../high-concurrent/mq-design.html">
            
                    
                    如果让你写一个消息队列，该如何进行架构设计啊？说一下你的思路。
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../high-concurrent/es-introduction.html">
            
                <a href="../high-concurrent/es-introduction.html">
            
                    
                    搜索引擎
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="../high-concurrent/es-architecture.html">
            
                <a href="../high-concurrent/es-architecture.html">
            
                    
                    es 的分布式架构原理能说一下么（es 是如何实现分布式的啊）？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="../high-concurrent/es-write-query-search.html">
            
                <a href="../high-concurrent/es-write-query-search.html">
            
                    
                    es 写入数据的工作原理是什么啊？es 查询数据的工作原理是什么啊？底层的 lucene 介绍一下呗？倒排索引了解吗？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.3" data-path="../high-concurrent/es-optimizing-query-performance.html">
            
                <a href="../high-concurrent/es-optimizing-query-performance.html">
            
                    
                    es 在数据量很大的情况下（数十亿级别）如何提高查询效率啊？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.4" data-path="../high-concurrent/es-production-cluster.html">
            
                <a href="../high-concurrent/es-production-cluster.html">
            
                    
                    es 生产集群的部署架构是什么？每个索引的数据量大概有多少？每个索引大概有多少个分片？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" >
            
                <span>
            
                    
                    缓存
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="../high-concurrent/why-cache.html">
            
                <a href="../high-concurrent/why-cache.html">
            
                    
                    在项目中缓存是如何使用的？缓存如果使用不当会造成什么后果？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="../high-concurrent/redis-single-thread-model.html">
            
                <a href="../high-concurrent/redis-single-thread-model.html">
            
                    
                    Redis 和 Memcached 有什么区别？Redis 的线程模型是什么？为什么单线程的 Redis 比多线程的 Memcached 效率要高得多？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.3" data-path="../high-concurrent/redis-data-types.html">
            
                <a href="../high-concurrent/redis-data-types.html">
            
                    
                    Redis 都有哪些数据类型？分别在哪些场景下使用比较合适？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.4" data-path="../high-concurrent/redis-expiration-policies-and-lru.html">
            
                <a href="../high-concurrent/redis-expiration-policies-and-lru.html">
            
                    
                    Redis 的过期策略都有哪些？手写一下 LRU 代码实现？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.5" data-path="../high-concurrent/how-to-ensure-high-concurrent-and-high-availability-of-redis.html">
            
                <a href="../high-concurrent/how-to-ensure-high-concurrent-and-high-availability-of-redis.html">
            
                    
                    如何保证 Redis 高并发、高可用？Redis 的主从复制原理能介绍一下么？Redis 的哨兵原理能介绍一下么？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.6" data-path="../high-concurrent/redis-persistence.html">
            
                <a href="../high-concurrent/redis-persistence.html">
            
                    
                    Redis 的持久化有哪几种方式？不同的持久化机制都有什么优缺点？持久化机制具体底层是如何实现的？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.7" data-path="../high-concurrent/redis-cluster.html">
            
                <a href="../high-concurrent/redis-cluster.html">
            
                    
                    Redis 集群模式的工作原理能说一下么？在集群模式下，Redis 的 key 是如何寻址的？分布式寻址都有哪些算法？了解一致性 hash 算法吗？如何动态增加和删除一个节点？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.8" data-path="../high-concurrent/redis-caching-avalanche-and-caching-penetration.html">
            
                <a href="../high-concurrent/redis-caching-avalanche-and-caching-penetration.html">
            
                    
                    了解什么是 redis 的雪崩、穿透和击穿？Redis 崩溃之后会怎么样？系统该如何应对这种情况？如何处理 Redis 的穿透？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.9" data-path="../high-concurrent/redis-consistence.html">
            
                <a href="../high-concurrent/redis-consistence.html">
            
                    
                    如何保证缓存与数据库的双写一致性？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.10" data-path="../high-concurrent/redis-cas.html">
            
                <a href="../high-concurrent/redis-cas.html">
            
                    
                    Redis 的并发竞争问题是什么？如何解决这个问题？了解 Redis 事务的 CAS 方案吗？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.11" data-path="../high-concurrent/redis-production-environment.html">
            
                <a href="../high-concurrent/redis-production-environment.html">
            
                    
                    生产环境中的 Redis 是怎么部署的？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" >
            
                <span>
            
                    
                    分库分表
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../high-concurrent/database-shard.html">
            
                <a href="../high-concurrent/database-shard.html">
            
                    
                    为什么要分库分表（设计高并发系统的时候，数据库层面该如何设计）？用过哪些分库分表中间件？不同的分库分表中间件都有什么优点和缺点？你们具体是如何对数据库如何进行垂直拆分或水平拆分的？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="../high-concurrent/database-shard-method.html">
            
                <a href="../high-concurrent/database-shard-method.html">
            
                    
                    现在有一个未分库分表的系统，未来要分库分表，如何设计才可以让系统从未分库分表动态切换到分库分表上？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="../high-concurrent/database-shard-dynamic-expand.html">
            
                <a href="../high-concurrent/database-shard-dynamic-expand.html">
            
                    
                    如何设计可以动态扩容缩容的分库分表方案？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.4" data-path="../high-concurrent/database-shard-global-id-generate.html">
            
                <a href="../high-concurrent/database-shard-global-id-generate.html">
            
                    
                    分库分表之后，id 主键如何处理？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" >
            
                <span>
            
                    
                    读写分离
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="../high-concurrent/mysql-read-write-separation.html">
            
                <a href="../high-concurrent/mysql-read-write-separation.html">
            
                    
                    如何实现 MySQL 的读写分离？MySQL 主从复制原理是啥？如何解决 MySQL 主从同步的延时问题？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" >
            
                <span>
            
                    
                    高并发系统
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" data-path="../high-concurrent/high-concurrent-design.html">
            
                <a href="../high-concurrent/high-concurrent-design.html">
            
                    
                    如何设计一个高并发系统？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">分布式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../distributed-system/distributed-system-interview.html">
            
                <a href="../distributed-system/distributed-system-interview.html">
            
                    
                    面试连环炮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" >
            
                <span>
            
                    
                    系统拆分
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="../distributed-system/why-dubbo.html">
            
                <a href="../distributed-system/why-dubbo.html">
            
                    
                    为什么要进行系统拆分？如何进行系统拆分？拆分后不用 Dubbo 可以吗？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" >
            
                <span>
            
                    
                    分布式服务框架
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="../distributed-system/dubbo-operating-principle.html">
            
                <a href="../distributed-system/dubbo-operating-principle.html">
            
                    
                    说一下 Dubbo 的工作原理？注册中心挂了可以继续通信吗？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="../distributed-system/dubbo-serialization-protocol.html">
            
                <a href="../distributed-system/dubbo-serialization-protocol.html">
            
                    
                    Dubbo 支持哪些序列化协议？说一下 Hessian 的数据结构？PB 知道吗？为什么 PB 的效率是最高的？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="../distributed-system/dubbo-load-balancing.html">
            
                <a href="../distributed-system/dubbo-load-balancing.html">
            
                    
                    Dubbo 负载均衡策略和集群容错策略都有哪些？动态代理策略呢？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="../distributed-system/dubbo-spi.html">
            
                <a href="../distributed-system/dubbo-spi.html">
            
                    
                    Dubbo 的 spi 思想是什么？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.5" data-path="../distributed-system/dubbo-service-management.html">
            
                <a href="../distributed-system/dubbo-service-management.html">
            
                    
                    如何基于 Dubbo 进行服务治理、服务降级、失败重试以及超时重试？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.6" data-path="../distributed-system/distributed-system-idempotency.html">
            
                <a href="../distributed-system/distributed-system-idempotency.html">
            
                    
                    分布式服务接口的幂等性如何设计（比如不能重复扣款）？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.7" data-path="../distributed-system/distributed-system-request-sequence.html">
            
                <a href="../distributed-system/distributed-system-request-sequence.html">
            
                    
                    分布式服务接口请求的顺序性如何保证？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.8" data-path="../distributed-system/dubbo-rpc-design.html">
            
                <a href="../distributed-system/dubbo-rpc-design.html">
            
                    
                    如何自己设计一个类似 Dubbo 的 RPC 框架？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" >
            
                <span>
            
                    
                    分布式锁
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="../distributed-system/zookeeper-application-scenarios.html">
            
                <a href="../distributed-system/zookeeper-application-scenarios.html">
            
                    
                    Zookeeper 都有哪些应用场景？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="../distributed-system/distributed-lock-redis-vs-zookeeper.html">
            
                <a href="../distributed-system/distributed-lock-redis-vs-zookeeper.html">
            
                    
                    使用 Redis 如何设计分布式锁？使用 Zookeeper 来设计分布式锁可以吗？以上两种分布式锁的实现方式哪种效率比较高？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" >
            
                <span>
            
                    
                    分布式事务
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.5.1" data-path="../distributed-system/distributed-transaction.html">
            
                <a href="../distributed-system/distributed-transaction.html">
            
                    
                    分布式事务了解吗？你们如何解决分布式事务问题的？TCC 如果出现网络连不通怎么办？XA 的一致性如何保证？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.6" >
            
                <span>
            
                    
                    分布式会话
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.6.1" data-path="../distributed-system/distributed-session.html">
            
                <a href="../distributed-system/distributed-session.html">
            
                    
                    集群部署时的分布式 Session 如何实现？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../high-availability/hystrix-introduction.html">
            
                <a href="../high-availability/hystrix-introduction.html">
            
                    
                    Hystrix 介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../high-availability/e-commerce-website-detail-page-architecture.html">
            
                <a href="../high-availability/e-commerce-website-detail-page-architecture.html">
            
                    
                    电商网站详情页系统架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="../high-availability/hystrix-thread-pool-isolation.html">
            
                <a href="../high-availability/hystrix-thread-pool-isolation.html">
            
                    
                    Hystrix 线程池技术实现资源隔离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="../high-availability/hystrix-semphore-isolation.html">
            
                <a href="../high-availability/hystrix-semphore-isolation.html">
            
                    
                    Hystrix 信号量机制实现资源隔离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.11" data-path="../high-availability/hystrix-execution-isolation.html">
            
                <a href="../high-availability/hystrix-execution-isolation.html">
            
                    
                    Hystrix 隔离策略细粒度控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.12" data-path="../high-availability/hystrix-process.html">
            
                <a href="../high-availability/hystrix-process.html">
            
                    
                    深入 Hystrix 执行时内部原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.13" data-path="../high-availability/hystrix-request-cache.html">
            
                <a href="../high-availability/hystrix-request-cache.html">
            
                    
                    基于 request cache 请求缓存技术优化批量商品数据查询接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.14" data-path="../high-availability/hystrix-fallback.html">
            
                <a href="../high-availability/hystrix-fallback.html">
            
                    
                    基于本地缓存的 fallback 降级机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.15" data-path="../high-availability/hystrix-circuit-breaker.html">
            
                <a href="../high-availability/hystrix-circuit-breaker.html">
            
                    
                    深入 Hystrix 断路器执行原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.16" data-path="../high-availability/hystrix-thread-pool-current-limiting.html">
            
                <a href="../high-availability/hystrix-thread-pool-current-limiting.html">
            
                    
                    深入 Hystrix 线程池隔离与接口限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.17" data-path="../high-availability/hystrix-timeout.html">
            
                <a href="../high-availability/hystrix-timeout.html">
            
                    
                    基于 timeout 机制为服务接口调用超时提供安全保护
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.18" >
            
                <span>
            
                    
                    高可用系统
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.18.1" >
            
                <span>
            
                    
                    如何设计一个高可用系统？
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.19" >
            
                <span>
            
                    
                    限流
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.19.1" data-path="../high-concurrent/huifer-how-to-limit-current.html">
            
                <a href="../high-concurrent/huifer-how-to-limit-current.html">
            
                    
                    如何限流？在工作中是怎么做的？说一下具体的实现？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.20" >
            
                <span>
            
                    
                    熔断
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.20.1" >
            
                <span>
            
                    
                    如何进行熔断？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="3.20.2" >
            
                <span>
            
                    
                    熔断框架都有哪些？具体实现原理知道吗？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="3.20.3" data-path="../high-availability/sentinel-vs-hystrix.html">
            
                <a href="../high-availability/sentinel-vs-hystrix.html">
            
                    
                    熔断框架如何做技术选型？选用 Sentinel 还是 Hystrix？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.21" >
            
                <span>
            
                    
                    降级
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.21.1" >
            
                <span>
            
                    
                    如何进行降级？
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">微服务架构</li>
        
        
    
        <li class="chapter " data-level="4.1" >
            
                <a target="_blank" href="https://github.com/doocs/advanced-java">
            
                    
                    微服务架构整个章节内容属额外新增，后续抽空更新，也欢迎读者们参与补充完善
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="microservices-introduction.html">
            
                <a href="microservices-introduction.html">
            
                    
                    关于微服务架构的描述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="migrating-from-a-monolithic-architecture-to-a-microservices-architecture.html">
            
                <a href="migrating-from-a-monolithic-architecture-to-a-microservices-architecture.html">
            
                    
                    从单体式架构迁移到微服务架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="event-driven-data-management-for-microservices.html">
            
                <a href="event-driven-data-management-for-microservices.html">
            
                    
                    微服务的事件驱动数据管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="choose-microservice-deployment-strategy.html">
            
                <a href="choose-microservice-deployment-strategy.html">
            
                    
                    选择微服务部署策略
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Spring Cloud 微服务架构</li>
        
        
    
        <li class="chapter active" data-level="5.1" data-path="huifer-what's-microservice-how-to-communicate.html">
            
                <a href="huifer-what's-microservice-how-to-communicate.html">
            
                    
                    什么是微服务？微服务之间是如何独立通讯的？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" >
            
                <span>
            
                    
                    Spring Cloud 和 Dubbo 有哪些区别？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" >
            
                <span>
            
                    
                    Spring Boot 和 Spring Cloud，谈谈你对它们的理解？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" >
            
                <span>
            
                    
                    什么是服务熔断？什么是服务降级？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" >
            
                <span>
            
                    
                    微服务的优缺点分别是什么？说一下你在项目开发中碰到的坑？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="huifer-micro-services-technology-stack.html">
            
                <a href="huifer-micro-services-technology-stack.html">
            
                    
                    你所知道的微服务技术栈都有哪些？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="huifer-micro-service-governance.html">
            
                <a href="huifer-micro-service-governance.html">
            
                    
                    微服务治理策略
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.8" >
            
                <span>
            
                    
                    Eureka 和 Zookeeper 都可以提供服务注册与发现的功能，它们有什么区别？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.9" >
            
                <span>
            
                    
                    ......
            
                </span>
            

            
        </li>
    

    
        
        <li class="header">海量数据处理</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../big-data/find-common-urls.html">
            
                <a href="../big-data/find-common-urls.html">
            
                    
                    如何从大量的 URL 中找出相同的 URL？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../big-data/find-top-100-words.html">
            
                <a href="../big-data/find-top-100-words.html">
            
                    
                    如何从大量数据中找出高频词？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../big-data/find-top-1-ip.html">
            
                <a href="../big-data/find-top-1-ip.html">
            
                    
                    如何找出某一天访问百度网站最多的 IP？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../big-data/find-no-repeat-number.html">
            
                <a href="../big-data/find-no-repeat-number.html">
            
                    
                    如何在大量的数据中找出不重复的整数？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="../big-data/find-a-number-if-exists.html">
            
                <a href="../big-data/find-a-number-if-exists.html">
            
                    
                    如何在大量的数据中判断一个数是否存在？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="../big-data/find-hotest-query-string.html">
            
                <a href="../big-data/find-hotest-query-string.html">
            
                    
                    如何查询最热门的查询串？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="../big-data/count-different-phone-numbers.html">
            
                <a href="../big-data/count-different-phone-numbers.html">
            
                    
                    如何统计不同电话号码的个数？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="../big-data/find-mid-value-in-500-millions.html">
            
                <a href="../big-data/find-mid-value-in-500-millions.html">
            
                    
                    如何从 5 亿个数中找出中位数？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.9" data-path="../big-data/sort-the-query-strings-by-counts.html">
            
                <a href="../big-data/sort-the-query-strings-by-counts.html">
            
                    
                    如何按照 query 的频度排序？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.10" data-path="../big-data/find-rank-top-500-numbers.html">
            
                <a href="../big-data/find-rank-top-500-numbers.html">
            
                    
                    如何找出排名前 500 的数？
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >什么是微服务？微服务之间是如何独立通讯的？</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x4EC0;&#x4E48;&#x662F;&#x5FAE;&#x670D;&#x52A1;&#xFF1F;&#x5FAE;&#x670D;&#x52A1;&#x4E4B;&#x95F4;&#x662F;&#x5982;&#x4F55;&#x72EC;&#x7ACB;&#x901A;&#x8BAF;&#x7684;&#xFF1F;">&#x4EC0;&#x4E48;&#x662F;&#x5FAE;&#x670D;&#x52A1;&#xFF1F;&#x5FAE;&#x670D;&#x52A1;&#x4E4B;&#x95F4;&#x662F;&#x5982;&#x4F55;&#x72EC;&#x7ACB;&#x901A;&#x8BAF;&#x7684;&#xFF1F;</h1>
<ul>
<li>Author: <a href="https://github.com/huifer" target="_blank">HuiFer</a></li>
<li>Description: &#x4ECB;&#x7ECD;&#x5FAE;&#x670D;&#x52A1;&#x7684;&#x5B9A;&#x4E49;&#x4EE5;&#x53CA;&#x670D;&#x52A1;&#x95F4;&#x7684;&#x901A;&#x4FE1;&#x3002;</li>
</ul>
<h2 id="&#x4EC0;&#x4E48;&#x662F;&#x5FAE;&#x670D;&#x52A1;">&#x4EC0;&#x4E48;&#x662F;&#x5FAE;&#x670D;&#x52A1;</h2>
<ul>
<li>&#x5FAE;&#x670D;&#x52A1;&#x67B6;&#x6784;&#x662F;&#x4E00;&#x4E2A;&#x5206;&#x5E03;&#x5F0F;&#x7CFB;&#x7EDF;, &#x6309;&#x7167;&#x4E1A;&#x52A1;&#x8FDB;&#x884C;&#x5212;&#x5206;&#x6210;&#x4E3A;&#x4E0D;&#x540C;&#x7684;&#x670D;&#x52A1;&#x5355;&#x5143;, &#x89E3;&#x51B3;&#x5355;&#x4F53;&#x7CFB;&#x7EDF;&#x6027;&#x80FD;&#x7B49;&#x4E0D;&#x8DB3;&#x3002;</li>
<li>&#x5FAE;&#x670D;&#x52A1;&#x662F;&#x4E00;&#x79CD;&#x67B6;&#x6784;&#x98CE;&#x683C;&#xFF0C;&#x4E00;&#x4E2A;&#x5927;&#x578B;&#x8F6F;&#x4EF6;&#x5E94;&#x7528;&#x7531;&#x591A;&#x4E2A;&#x670D;&#x52A1;&#x5355;&#x5143;&#x7EC4;&#x6210;&#x3002;&#x7CFB;&#x7EDF;&#x4E2D;&#x7684;&#x670D;&#x52A1;&#x5355;&#x5143;&#x53EF;&#x4EE5;&#x5355;&#x72EC;&#x90E8;&#x7F72;&#xFF0C;&#x5404;&#x4E2A;&#x670D;&#x52A1;&#x5355;&#x5143;&#x4E4B;&#x95F4;&#x662F;&#x677E;&#x8026;&#x5408;&#x7684;&#x3002;</li>
</ul>
<blockquote>
<p>&#x5FAE;&#x670D;&#x52A1;&#x6982;&#x5FF5;&#x8D77;&#x6E90;:  <a href="https://martinfowler.com/articles/microservices.html" target="_blank">Microservices</a></p>
</blockquote>
<h2 id="&#x5FAE;&#x670D;&#x52A1;&#x4E4B;&#x95F4;&#x662F;&#x5982;&#x4F55;&#x72EC;&#x7ACB;&#x901A;&#x8BAF;&#x7684;">&#x5FAE;&#x670D;&#x52A1;&#x4E4B;&#x95F4;&#x662F;&#x5982;&#x4F55;&#x72EC;&#x7ACB;&#x901A;&#x8BAF;&#x7684;</h2>
<h3 id="&#x540C;&#x6B65;">&#x540C;&#x6B65;</h3>
<h4 id="rest-http-&#x534F;&#x8BAE;">REST HTTP &#x534F;&#x8BAE;</h4>
<p>REST &#x8BF7;&#x6C42;&#x5728;&#x5FAE;&#x670D;&#x52A1;&#x4E2D;&#x662F;&#x6700;&#x4E3A;&#x5E38;&#x7528;&#x7684;&#x4E00;&#x79CD;&#x901A;&#x8BAF;&#x65B9;&#x5F0F;, &#x5B83;&#x4F9D;&#x8D56;&#x4E8E; HTTP\HTTPS &#x534F;&#x8BAE;&#x3002;RESTFUL &#x7684;&#x7279;&#x70B9;&#x662F;&#xFF1A;</p>
<ol>
<li>&#x6BCF;&#x4E00;&#x4E2A; URI &#x4EE3;&#x8868; 1 &#x79CD;&#x8D44;&#x6E90;</li>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x4F7F;&#x7528; GET&#x3001;POST&#x3001;PUT&#x3001;DELETE 4 &#x4E2A;&#x8868;&#x793A;&#x64CD;&#x4F5C;&#x65B9;&#x5F0F;&#x7684;&#x52A8;&#x8BCD;&#x5BF9;&#x670D;&#x52A1;&#x7AEF;&#x8D44;&#x6E90;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;:  GET &#x7528;&#x6765;&#x83B7;&#x53D6;&#x8D44;&#x6E90;, POST &#x7528;&#x6765;&#x65B0;&#x5EFA;&#x8D44;&#x6E90;(&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x66F4;&#x65B0;&#x8D44;&#x6E90;), PUT &#x7528;&#x6765;&#x66F4;&#x65B0;&#x8D44;&#x6E90;, DELETE &#x7528;&#x6765;&#x5220;&#x9664;&#x8D44;&#x6E90;</li>
<li>&#x901A;&#x8FC7;&#x64CD;&#x4F5C;&#x8D44;&#x6E90;&#x7684;&#x8868;&#x73B0;&#x5F62;&#x5F0F;&#x6765;&#x64CD;&#x4F5C;&#x8D44;&#x6E90;</li>
<li>&#x8D44;&#x6E90;&#x7684;&#x8868;&#x73B0;&#x5F62;&#x5F0F;&#x662F; XML &#x6216;&#x8005; HTML</li>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x4E0E;&#x670D;&#x52A1;&#x7AEF;&#x4E4B;&#x95F4;&#x7684;&#x4EA4;&#x4E92;&#x5728;&#x8BF7;&#x6C42;&#x4E4B;&#x95F4;&#x662F;&#x65E0;&#x72B6;&#x6001;&#x7684;,&#x4ECE;&#x5BA2;&#x6237;&#x7AEF;&#x5230;&#x670D;&#x52A1;&#x7AEF;&#x7684;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x90FD;&#x5FC5;&#x987B;&#x5305;&#x542B;&#x7406;&#x89E3;&#x8BF7;&#x6C42;&#x6240;&#x5FC5;&#x9700;&#x7684;&#x4FE1;&#x606F;</li>
</ol>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#x65B9;&#x63D0;&#x4F9B;&#x4E86;&#x5982;&#x4E0B;&#x63A5;&#x53E3;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-meta">@RestController</span>
<span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">&quot;/communication&quot;</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RestControllerDemo</span> </span>{
    <span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">&quot;/hello&quot;</span>)
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">s</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello&quot;</span>;
    }
}
</code></pre>
<p>&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#x9700;&#x8981;&#x53BB;&#x8C03;&#x7528;&#x8BE5;&#x63A5;&#x53E3;&#xFF0C;&#x8C03;&#x7528;&#x65B9;&#x53EA;&#x9700;&#x8981;&#x6839;&#x636E; API &#x6587;&#x6863;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x5373;&#x53EF;&#x83B7;&#x53D6;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-meta">@RestController</span>
<span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">&quot;/demo&quot;</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RestDemo</span></span>{
    <span class="hljs-meta">@Autowired</span>
    RestTemplate restTemplate;

    <span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">&quot;/hello2&quot;</span>)
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">s2</span><span class="hljs-params">()</span> </span>{
        String forObject = restTemplate.getForObject(<span class="hljs-string">&quot;http://localhost:9013/communication/hello&quot;</span>, String.class);
        <span class="hljs-keyword">return</span> forObject;
    }
}
</code></pre>
<p>&#x901A;&#x8FC7;&#x8FD9;&#x6837;&#x7684;&#x65B9;&#x5F0F;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x670D;&#x52A1;&#x4E4B;&#x95F4;&#x7684;&#x901A;&#x8BAF;&#x3002;</p>
<h4 id="rpc-tcp&#x534F;&#x8BAE;">RPC TCP&#x534F;&#x8BAE;</h4>
<p>RPC(Remote Procedure Call)&#x8FDC;&#x7A0B;&#x8FC7;&#x7A0B;&#x8C03;&#x7528;&#xFF0C;&#x7B80;&#x5355;&#x7684;&#x7406;&#x89E3;&#x662F;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x8BF7;&#x6C42;&#x53E6;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x63D0;&#x4F9B;&#x7684;&#x670D;&#x52A1;&#x3002;&#x5B83;&#x7684;&#x5DE5;&#x4F5C;&#x6D41;&#x7A0B;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;</p>
<ol>
<li>&#x6267;&#x884C;&#x5BA2;&#x6237;&#x7AEF;&#x8C03;&#x7528;&#x8BED;&#x53E5;&#xFF0C;&#x4F20;&#x9001;&#x53C2;&#x6570;</li>
<li>&#x8C03;&#x7528;&#x672C;&#x5730;&#x7CFB;&#x7EDF;&#x53D1;&#x9001;&#x7F51;&#x7EDC;&#x6D88;&#x606F;</li>
<li>&#x6D88;&#x606F;&#x4F20;&#x9001;&#x5230;&#x8FDC;&#x7A0B;&#x4E3B;&#x673A;</li>
<li>&#x670D;&#x52A1;&#x5668;&#x5F97;&#x5230;&#x6D88;&#x606F;&#x5E76;&#x53D6;&#x5F97;&#x53C2;&#x6570;</li>
<li>&#x6839;&#x636E;&#x8C03;&#x7528;&#x8BF7;&#x6C42;&#x4EE5;&#x53CA;&#x53C2;&#x6570;&#x6267;&#x884C;&#x8FDC;&#x7A0B;&#x8FC7;&#x7A0B;&#xFF08;&#x670D;&#x52A1;&#xFF09;</li>
<li>&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x5B8C;&#x6BD5;&#xFF0C;&#x5C06;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#x670D;&#x52A1;&#x5668;&#x53E5;&#x67C4;</li>
<li>&#x670D;&#x52A1;&#x5668;&#x53E5;&#x67C4;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#xFF0C;&#x8C03;&#x7528;&#x8FDC;&#x7A0B;&#x4E3B;&#x673A;&#x7684;&#x7CFB;&#x7EDF;&#x7F51;&#x7EDC;&#x670D;&#x52A1;&#x53D1;&#x9001;&#x7ED3;&#x679C;</li>
<li>&#x6D88;&#x606F;&#x4F20;&#x56DE;&#x672C;&#x5730;&#x4E3B;&#x673A;</li>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x53E5;&#x67C4;&#x7531;&#x672C;&#x5730;&#x4E3B;&#x673A;&#x7684;&#x7F51;&#x7EDC;&#x670D;&#x52A1;&#x63A5;&#x6536;&#x6D88;&#x606F;</li>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x63A5;&#x6536;&#x5230;&#x8C03;&#x7528;&#x8BED;&#x53E5;&#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x679C;&#x6570;&#x636E;</li>
</ol>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<p>&#x9996;&#x5148;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#x7AEF;&#xFF1A;</p>
<pre><code class="lang-java">
<span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> java.io.ObjectInputStream;
<span class="hljs-keyword">import</span> java.io.ObjectOutputStream;
<span class="hljs-keyword">import</span> java.lang.reflect.Method;
<span class="hljs-keyword">import</span> java.net.InetSocketAddress;
<span class="hljs-keyword">import</span> java.net.ServerSocket;
<span class="hljs-keyword">import</span> java.net.Socket;
<span class="hljs-keyword">import</span> java.util.concurrent.ConcurrentHashMap;
<span class="hljs-keyword">import</span> java.util.concurrent.ExecutorService;
<span class="hljs-keyword">import</span> java.util.concurrent.Executors;

<span class="hljs-comment">/**
 * RPC &#x670D;&#x52A1;&#x7AEF;&#x7528;&#x6765;&#x6CE8;&#x518C;&#x8FDC;&#x7A0B;&#x65B9;&#x6CD5;&#x7684;&#x63A5;&#x53E3;&#x548C;&#x5B9E;&#x73B0;&#x7C7B;
 * <span class="hljs-doctag">@Date</span>: 2019-11-04
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RPCServer</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> ConcurrentHashMap&lt;String, Class&gt; serviceRegister = <span class="hljs-keyword">new</span> ConcurrentHashMap&lt;&gt;();

    <span class="hljs-comment">/**
     * &#x6CE8;&#x518C;&#x65B9;&#x6CD5;
     * <span class="hljs-doctag">@param</span> service
     * <span class="hljs-doctag">@param</span> impl
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">register</span><span class="hljs-params">(Class service, Class impl)</span> </span>{
        serviceRegister.put(service.getSimpleName(), impl);
    }

    <span class="hljs-comment">/**
     * &#x542F;&#x52A8;&#x65B9;&#x6CD5;
     * <span class="hljs-doctag">@param</span> port
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">start</span><span class="hljs-params">(<span class="hljs-keyword">int</span> port)</span> </span>{
        ServerSocket socket = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">try</span> {
            socket = <span class="hljs-keyword">new</span> ServerSocket();
            socket.bind(<span class="hljs-keyword">new</span> InetSocketAddress(port));
            System.out.println(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x542F;&#x52A8;&quot;</span>);
            System.out.println(serviceRegister);
            <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>) {
                executor.execute(<span class="hljs-keyword">new</span> Task(socket.accept()));
            }
        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        } <span class="hljs-keyword">finally</span> {
            <span class="hljs-keyword">if</span> (socket != <span class="hljs-keyword">null</span>) {
                <span class="hljs-keyword">try</span> {
                    socket.close();
                } <span class="hljs-keyword">catch</span> (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Task</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Runnable</span> </span>{
        Socket client = <span class="hljs-keyword">null</span>;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Task</span><span class="hljs-params">(Socket client)</span> </span>{
            <span class="hljs-keyword">this</span>.client = client;
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            ObjectInputStream input = <span class="hljs-keyword">null</span>;
            ObjectOutputStream output = <span class="hljs-keyword">null</span>;
            <span class="hljs-keyword">try</span> {
                input = <span class="hljs-keyword">new</span> ObjectInputStream(client.getInputStream());
                <span class="hljs-comment">// &#x6309;&#x7167;&#x987A;&#x5E8F;&#x8BFB;&#x53D6;&#x5BF9;&#x65B9;&#x5199;&#x8FC7;&#x6765;&#x7684;&#x5185;&#x5BB9;</span>
                String serviceName = input.readUTF();
                String methodName = input.readUTF();
                Class&lt;?&gt;[] parameterTypes = (Class&lt;?&gt;[]) input.readObject();
                Object[] arguments = (Object[]) input.readObject();
                Class serviceClass = serviceRegister.get(serviceName);
                <span class="hljs-keyword">if</span> (serviceClass == <span class="hljs-keyword">null</span>) {
                    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ClassNotFoundException(serviceName + <span class="hljs-string">&quot; &#x6CA1;&#x6709;&#x627E;&#x5230;!&quot;</span>);
                }
                Method method = serviceClass.getMethod(methodName, parameterTypes);
                Object result = method.invoke(serviceClass.newInstance(), arguments);

                output = <span class="hljs-keyword">new</span> ObjectOutputStream(client.getOutputStream());
                output.writeObject(result);
            } <span class="hljs-keyword">catch</span> (Exception e) {
                e.printStackTrace();

            } <span class="hljs-keyword">finally</span> {
                <span class="hljs-keyword">try</span> {
                    <span class="hljs-comment">// &#x8FD9;&#x91CC;&#x5C31;&#x4E0D;&#x5199; output!=null&#x624D;&#x5173;&#x95ED;&#x8FD9;&#x4E2A;&#x903B;&#x8F91;&#x4E86;</span>
                    output.close();
                    input.close();
                    client.close();
                } <span class="hljs-keyword">catch</span> (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}
</code></pre>
<p>&#x5176;&#x6B21;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#xFF1A;</p>
<pre><code class="lang-java">
<span class="hljs-keyword">import</span> java.io.ObjectInputStream;
<span class="hljs-keyword">import</span> java.io.ObjectOutputStream;
<span class="hljs-keyword">import</span> java.lang.reflect.InvocationHandler;
<span class="hljs-keyword">import</span> java.lang.reflect.Method;
<span class="hljs-keyword">import</span> java.lang.reflect.Proxy;
<span class="hljs-keyword">import</span> java.net.InetSocketAddress;
<span class="hljs-keyword">import</span> java.net.Socket;

<span class="hljs-comment">/**
 * RPC &#x5BA2;&#x6237;&#x7AEF;
 * <span class="hljs-doctag">@Date</span>: 2019-11-04
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RPCclient</span>&lt;<span class="hljs-title">T</span>&gt; </span>{
    <span class="hljs-comment">/**
     * &#x901A;&#x8FC7;&#x52A8;&#x6001;&#x4EE3;&#x7406;&#x5C06;&#x53C2;&#x6570;&#x53D1;&#x9001;&#x8FC7;&#x53BB;&#x5230; RPCServer ,RPCserver &#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5904;&#x7406;&#x6210;&#x4E3A;&#x6B63;&#x786E;&#x7684;&#x5B9E;&#x4F53;
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; <span class="hljs-function">T <span class="hljs-title">getRemoteProxyObj</span><span class="hljs-params">(<span class="hljs-keyword">final</span> Class&lt;T&gt; service, <span class="hljs-keyword">final</span> InetSocketAddress addr)</span> </span>{

        <span class="hljs-keyword">return</span> (T) Proxy.newProxyInstance(service.getClassLoader(), <span class="hljs-keyword">new</span> Class&lt;?&gt;[]{service}, <span class="hljs-keyword">new</span> InvocationHandler() {
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">invoke</span><span class="hljs-params">(Object proxy, Method method, Object[] args)</span> <span class="hljs-keyword">throws</span> Throwable </span>{

                Socket socket = <span class="hljs-keyword">null</span>;
                ObjectOutputStream out = <span class="hljs-keyword">null</span>;
                ObjectInputStream input = <span class="hljs-keyword">null</span>;
                <span class="hljs-keyword">try</span> {
                    socket = <span class="hljs-keyword">new</span> Socket();
                    socket.connect(addr);

                    <span class="hljs-comment">// &#x5C06;&#x5B9E;&#x4F53;&#x7C7B;,&#x53C2;&#x6570;,&#x53D1;&#x9001;&#x7ED9;&#x8FDC;&#x7A0B;&#x8C03;&#x7528;&#x65B9;</span>
                    out = <span class="hljs-keyword">new</span> ObjectOutputStream(socket.getOutputStream());
                    out.writeUTF(service.getSimpleName());
                    out.writeUTF(method.getName());
                    out.writeObject(method.getParameterTypes());
                    out.writeObject(args);

                    input = <span class="hljs-keyword">new</span> ObjectInputStream(socket.getInputStream());
                    <span class="hljs-keyword">return</span> input.readObject();
                } <span class="hljs-keyword">catch</span> (Exception e) {
                    e.printStackTrace();
                } <span class="hljs-keyword">finally</span> {
                    out.close();
                    input.close();
                    socket.close();
                }
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
            }
        });

    }

}
</code></pre>
<p>&#x518D;&#x6765;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x7684;&#x8FDC;&#x7A0B;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Tinterface</span> </span>{
    <span class="hljs-function">String <span class="hljs-title">send</span><span class="hljs-params">(String msg)</span></span>;
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TinterfaceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Tinterface</span> </span>{
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">send</span><span class="hljs-params">(String msg)</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;send message &quot;</span> + msg;
    }
}
</code></pre>
<p>&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">
<span class="hljs-keyword">import</span> com.huifer.admin.rpc.Tinterface;
<span class="hljs-keyword">import</span> com.huifer.admin.rpc.TinterfaceImpl;

<span class="hljs-keyword">import</span> java.net.InetSocketAddress;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@Date</span>: 2019-11-04
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RunTest</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> Runnable() {
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
                RPCServer rpcServer = <span class="hljs-keyword">new</span> RPCServer();
                rpcServer.register(Tinterface.class, TinterfaceImpl.class);
                rpcServer.start(<span class="hljs-number">10000</span>);
            }
        }).start();
        Tinterface tinterface = RPCclient.getRemoteProxyObj(Tinterface.class, <span class="hljs-keyword">new</span> InetSocketAddress(<span class="hljs-string">&quot;localhost&quot;</span>, <span class="hljs-number">10000</span>));
        System.out.println(tinterface.send(<span class="hljs-string">&quot;rpc &#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&quot;</span>));


    }
}
</code></pre>
<p>&#x8F93;&#x51FA; <code>send message rpc &#x6D4B;&#x8BD5;&#x7528;&#x4F8B;</code>&#x3002;</p>
<h3 id="&#x5F02;&#x6B65;">&#x5F02;&#x6B65;</h3>
<h4 id="&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;">&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;</h4>
<blockquote>
<p>&#x5E38;&#x89C1;&#x7684;&#x6D88;&#x606F;&#x4E2D;&#x95F4;&#x4EF6;&#x6709; Kafka&#x3001;ActiveMQ&#x3001;RabbitMQ&#x3001;RocketMQ , &#x5E38;&#x89C1;&#x7684;&#x534F;&#x8BAE;&#x6709;AMQP&#x3001;MQTTP&#x3001;STOMP&#x3001;XMPP. &#x8FD9;&#x91CC;&#x4E0D;&#x5BF9;&#x6D88;&#x606F;&#x961F;&#x5217;&#x8FDB;&#x884C;&#x62D3;&#x5C55;&#x4E86;, &#x5177;&#x4F53;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x8FD8;&#x662F;&#x8BF7;&#x79FB;&#x6B65;&#x5B98;&#x7F51;.</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="choose-microservice-deployment-strategy.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 选择微服务部署策略">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"什么是微服务？微服务之间是如何独立通讯的？","level":"5.1","depth":1,"next":{"title":"Spring Cloud 和 Dubbo 有哪些区别？","level":"5.2","depth":1,"ref":"","articles":[]},"previous":{"title":"选择微服务部署策略","level":"4.5","depth":1,"path":"micro-services/choose-microservice-deployment-strategy.md","ref":"micro-services/choose-microservice-deployment-strategy.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"micro-services/huifer-what's-microservice-how-to-communicate.md","mtime":"2019-12-30T01:36:31.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-01-08T09:33:59.173Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

